package com.phs.main.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phs.main.common.constant.AjaxResult;
import com.phs.main.web.po.*;
import com.phs.main.web.service.IPropertyInformationService;
import com.phs.main.web.service.IServicesTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 服务表 前端控制器
 * </p>
 *
 * @author Trent
 * @since 2020-07-07
 */
@Api
@RestController
@RequestMapping("/servicesTable")
public class ServicesTableController {
	@Lazy
	@Autowired
	IServicesTableService servicesTableService;
	@Lazy
	@Autowired
	IPropertyInformationService propertyInformationService;

	@ApiOperation(value = "添加服务记录")
	@PostMapping("/addServices")
	public AjaxResult addServices(@RequestBody Map<String, String> map) {
		try {
			QueryWrapper<PropertyInformation> propInfoWrapper = new QueryWrapper<>();
			propInfoWrapper.eq("house_unit", map.get("houseUnit"))
					.eq("house_name", map.get("houseName"));
			PropertyInformation one = propertyInformationService.getOne(propInfoWrapper);
			Date t = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			System.out.println(sdf.format(t));
			DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

			LocalDateTime ldt = LocalDateTime.parse(sdf.format(t),df);
			System.out.println("String类型的时间转成LocalDateTime："+map.get("data2"));
			ServicesTable services = new ServicesTable();
			services.setUserName(map.get("userName"));
			services.setPhone(map.get("phone"));
			services.setRequireDetail(map.get("requireDetail"));
			services.setUserId(((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());
			services.setSTime(ldt);
			services.setHouseId(one.getHouseId());
			servicesTableService.save(services);
			return AjaxResult.success("添加成功");
		} catch (Exception e) {
			return AjaxResult.error(e.getMessage());
		}
	}

	@ApiOperation(value = "删除", notes = "根据ID删除服务")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "服务ID", dataType = "Long", required = true),
	})
	@DeleteMapping("deleteServicesTableById/{id}")
	public AjaxResult deleteServicesTableById(@PathVariable long id) {
		try {
			if (servicesTableService.removeById(id)) {
				return AjaxResult.success("已删除");
			} else {
				return AjaxResult.error("未删除");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("更新服务")
	@PutMapping("/updateServicesTable")
	public AjaxResult updateServicesTable(@RequestBody ServicesTable servicesTable) {
		try {
			if (servicesTableService.updateById(servicesTable)) {
				return AjaxResult.success("更改成功");
			} else {
				return AjaxResult.error("更改失败");
			}
		} catch (Exception e) {
			return AjaxResult.error("出错", e.getMessage());
		}
	}

	@ApiOperation("查询所有服务")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current", value = "页码", dataType = "Long"),
			@ApiImplicitParam(name = "size", value = "单页条数", dataType = "Long")
	})
	@GetMapping("/findAll/{current}/{size}")
	public AjaxResult findAll(@PathVariable Long current, @PathVariable(required = false) Long size) {
		if (current == null) {
			current = 1L;
		}
		if (size == null) {
			size = 10L;
		}
		Page<ServicesTable> page = new Page<>(current, size);

		return AjaxResult.success(servicesTableService.page(page));
	}

	@ApiOperation("按照服务状态查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sStatus", value = "服务状态", dataType = "Long")
	})
	@GetMapping("/findByStatus/{sStatus}")
	public AjaxResult findByStatus(@PathVariable Long sStatus) {
		QueryWrapper<ServicesTable> servicesTableQueryWrapper = new QueryWrapper<>();
		servicesTableQueryWrapper.like("s_status", sStatus);

		return AjaxResult.success(servicesTableService.list(servicesTableQueryWrapper));
	}

	@ApiOperation("按照服务类型查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "serviceType", value = "服务类型", dataType = "Long"),
	})
	@GetMapping("/findByFrom/{serviceType}")
	public AjaxResult findByFrom(@PathVariable Long serviceType) {
		QueryWrapper<ServicesTable> servicesTableQueryWrapper = new QueryWrapper<>();
		servicesTableQueryWrapper.like("service_type", serviceType);

		return AjaxResult.success(servicesTableService.list(servicesTableQueryWrapper));
	}

	@ApiOperation("按照用户id服务状态查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sStatus", value = "服务状态", dataType = "Long")
	})
	@GetMapping("/findByIDStatus/{sStatus}")
	public AjaxResult findByIDStatus(@PathVariable Long sStatus) {
		QueryWrapper<ServicesTable> servicesTableQueryWrapper = new QueryWrapper<>();
		servicesTableQueryWrapper.like("user_ID", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId()).like("s_status", sStatus);

		return AjaxResult.success(servicesTableService.list(servicesTableQueryWrapper));
	}

	@ApiOperation("按照用户id服务类型查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "serviceType", value = "服务类型", dataType = "Long"),
	})
	@GetMapping("/findByIDFrom/{serviceType}")
	public AjaxResult findByIDFrom(@PathVariable Long serviceType) {
		QueryWrapper<ServicesTable> servicesTableQueryWrapper = new QueryWrapper<>();
		servicesTableQueryWrapper.like("user_ID", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId()).like("service_type", serviceType);

		return AjaxResult.success(servicesTableService.list(servicesTableQueryWrapper));
	}

	@ApiOperation("按照用户id查询")
	@ApiImplicitParams({

	})
	@GetMapping("/findByID")
	public AjaxResult findByID() {
		QueryWrapper<ServicesTable> servicesTableQueryWrapper = new QueryWrapper<>();
		servicesTableQueryWrapper.like("user_id", ((SysUsers) SecurityUtils.getSubject().getPrincipal()).getUserId());

		return AjaxResult.success(servicesTableService.list(servicesTableQueryWrapper));
	}
}
